package com.tedu.controller;

import com.tedu.entry.User;
import com.tedu.entry.vo.UserVO;
import com.tedu.service.UserService;
import com.tedu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Author：毕龙
 * Date: 2023/7/28 10:28
 */
@WebServlet("/user/login")
public class UserLoginController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = null;
        try {
            // 1.接收客户端的数据  处理请求
            //处理中文乱码
            System.out.println("嘿嘿");
            request.setCharacterEncoding("utf-8");
            //获取username password
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            System.out.println(username);
            System.out.println(password);
            UserVO vo = new UserVO();
            session = request.getSession();

            vo.setUsername(username).setPassword(password);

            //2.调用业务逻辑  service
            UserService userService = new UserServiceImpl();
            User user = userService.selectUserByUP(vo);

            //3.处理响应结果
            //重定向
            response.sendRedirect(request.getContextPath()+"/emp/findAll");

        } catch (Exception e) {
            //获取异常信息
            String message = e.getMessage();
            System.out.println(message);
            //将异常信息存放在session作用域中
            System.out.println("呵呵");
            session = request.getSession();
            session.setAttribute("message",message);
            Object message1 = session.getAttribute("message");
            System.out.println(session.getId());
            System.out.println(message1);
            response.sendRedirect(request.getContextPath()+"/ems/user/login.jsp");
        } finally {
        }

    }
}
